/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package json;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author teste
 */
public class SimpleJson {
    ResultSet rs;
    String[] columnNames;
    int columnsLen;
    private ArrayList content;

    public SimpleJson(){
        this.content = new ArrayList();
    }
    
    public void setResultSet(ResultSet rs){
        this.rs = rs;
        
        try{
            this.resolveColumnNames();
        }catch(SQLException e){
            
        }
    }
    
    private void resolveColumnNames() throws SQLException{
        try{
            //descobre o nome das colunas
            ResultSetMetaData rsmd = this.rs.getMetaData();
            int columnsLen = rsmd.getColumnCount();
            String[] columnNames = new String[columnsLen];
            
            for (int i = 1; i<columnsLen+1; i++) {//começa do 1 e não do 0
                String columnName = rsmd.getColumnName(i);
                columnNames[i-1] = columnName;
            }
            
            this.columnNames = columnNames;
            this.columnsLen = columnsLen;
        }catch(SQLException e){
            
        }
    }
    
    public void clearContent(){
        this.content = new ArrayList();
    }
    
    private void appendContent(String newContent){
        this.content.add(newContent);
    }
    
    public String getContent(){
        int contentLength = this.content.size();
        String joined = this.join(this.content);
        if(contentLength > 1) joined = "["+joined+"]";
        
        return joined;
    }
    
    private String join(ArrayList list){
        String joined = "";
        for(int i = 0; i < list.size(); i++){
            if(i != 0) joined += ",";
            joined += list.get(i);
        }
        
        return joined;
    }
    
    public void generate() throws SQLException{
        String json = "";
        String value;

        int commaCount = 0;
        try{
            while(this.rs.next()){
                //se não for o primeiro, acrescenta vírgula ao início, para separar um conjunto
                if(commaCount != 0) json += ",";
                commaCount++;

                json += "[";

                for(int i = 0; i<this.columnsLen; i++){
                    value = this.rs.getString(this.columnNames[i]);
                    //se for número, não põe aspas
                   // if(!this.isNumeric(value)) <-- verificar qual é o erro
                    value = "\""+value+"\"";
                    //acrescenta na String JSON
                    json += value;
                    //se não for o último, acrescenta vírgula
                    if(i != this.columnsLen-1) json += ",";
                }

                json += "]";
            }            
        }catch(SQLException e){
        
        }
        
        this.appendContent("["+json+"]");
    }
    
    private boolean isNumeric(String value){
        boolean isNumeric = false;
        try{
            Float.parseFloat(value);
            isNumeric = true;
        }catch(Exception e){
            
        }finally{
            return isNumeric;
        }
    }
}